package servlet;

import vo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(value = "/forwardLogin")
public class Demo9_forwardLoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if(username.equals("tiger") && password.equals("scott")){

            request.setAttribute("result","请求登录成功");
            //转发的时候需要携带的数据
            session.setAttribute("result","登录成功");

            request.setAttribute("user",new User("tom",100));

        }else{
            //转发的时候需要携带的数据
            session.setAttribute("result","登录失败");
        }

        //携带数据转发到 login.jsp页面
        request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);


    }
}
